﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Collections;

namespace Acsys.Entity
{
    public class PaymentDebitTransaction : IEntity
    {
        public int ID = 0;
        public string CODE = "D00000";
        public string NAME = "-";
        public Currency CURRENCY = null;
        public double AMOUNT = 0d;
        public DocumentType DOCUMENT_TYPE = DocumentType.Cash;
        public DateTime TRANSACTION_DATE = DateTime.Today;
        public string BANK = "";
        public DebitTransaction DEBIT_TRANSACTION = null;

        public PaymentDebitTransaction()
        {
        }
        public PaymentDebitTransaction(int id)
        {
            ID = id;
        }
        public PaymentDebitTransaction(int id, string code)
        {
            ID = id;
            CODE = code;
        }
        public IEntity Get(MySqlDataReader aReader)
        {
            PaymentDebitTransaction ccy = null;
            while (aReader.Read())
            {
                ccy = new PaymentDebitTransaction();
                ccy.ID = Convert.ToInt32(aReader[0]);
                ccy.CODE = aReader[1].ToString();
                ccy.NAME = aReader[2].ToString();
                ccy.CURRENCY = new Currency(Convert.ToInt32(aReader[3]));
                ccy.AMOUNT = Convert.ToDouble(aReader[4]);
                ccy.DOCUMENT_TYPE = (DocumentType)Enum.Parse(typeof(DocumentType), aReader[5].ToString());
                ccy.TRANSACTION_DATE = Convert.ToDateTime(aReader[6]);
                ccy.BANK = aReader[7].ToString();
                ccy.DEBIT_TRANSACTION = new DebitTransaction(Convert.ToInt32(aReader[8]));
            }
            aReader.Close();
            return ccy;
        }
        public string GetInsertSQL()
        {
            return String.Format(@"insert into table_paymentdebit (pyd_code,pyd_name,ccy_id,pyd_amount,pyd_doctype,pyd_date,pyd_bank,drtr_id) 
                VALUES ('{0}','{1}',{2},{3},'{4}','{5}','{6}',{7})",
                CODE, 
                NAME,
                CURRENCY.ID,
                AMOUNT,
                DOCUMENT_TYPE.ToString(),
                TRANSACTION_DATE.ToString("yyyy/MM/dd"),
                BANK,
                DEBIT_TRANSACTION.ID
                );
        }
        public string GetDeleteSQL()
        {
            return "delete from table_paymentdebit where pyd_id = " + ID;
        }
        public string GetUpdateSQL()
        {
            return String.Format(@"update table_paymentdebit set 
                pyd_code = '{0}', 
                pyd_name='{1}',
                ccy_id={2},
                pyd_amount={3},
                pyd_doctype='{4}',
                pyd_date='{5}',
                pyd_bank='{6}',
                drtr_id = {7}
                where pyd_id = {8}",
                CODE, NAME, 
                CURRENCY.ID,
                AMOUNT,
                DOCUMENT_TYPE.ToString(),
                TRANSACTION_DATE.ToString("yyyy/MM/dd"),
                BANK,
                DEBIT_TRANSACTION.ID,
                ID);
        }
        public string GetByIDSQL(int ID)
        {
            return String.Format("select * from table_paymentdebit where pyd_id = {0}", ID);
        }
        public string GetByCodeSQL(string code)
        {
            return String.Format("select * from table_paymentdebit where pyd_code = '{0}'", code);
        }
        public string GetByCodeLikeSQL(string text)
        {
            return String.Format("select * from table_paymentdebit where pyd_code like '%{0}%'", text);
        }
        public string GetByNameLikeSQL(string text)
        {
            return String.Format("select * from table_paymentdebit where pyd_name like '%{0}%'", text);
        }
        public string GetAllSQL()
        {
            return String.Format("select * from table_paymentdebit");
        }
        public IList GetAll(MySqlDataReader aReader)
        {
            IList result = new ArrayList();
            while (aReader.Read())
            {
                PaymentDebitTransaction des = new PaymentDebitTransaction();
                des.ID = Convert.ToInt32(aReader[0]);
                des.CODE = aReader[1].ToString();
                des.NAME = aReader[2].ToString();
                des.CURRENCY = new Currency(Convert.ToInt32(aReader[3]));
                des.AMOUNT = Convert.ToDouble(aReader[4]);
                des.DOCUMENT_TYPE = (DocumentType)Enum.Parse(typeof(DocumentType), aReader[5].ToString());
                des.TRANSACTION_DATE = Convert.ToDateTime(aReader[6]);
                des.BANK = aReader[7].ToString();
                des.DEBIT_TRANSACTION = new DebitTransaction(Convert.ToInt32(aReader[8]));
                result.Add(des);
            }
            aReader.Close();
            return result;
        }
        public int GetID()
        {
            return ID;
        }
        public void SetID(int id)
        {
            ID = id;
        }
        public override string ToString()
        {
            return CODE;
        }
        public string GetCode()
        {
            return CODE;
        }
        public void SetCode(string code)
        {
            CODE = code;
        }
        public string GetMaximumIDSQL()
        {
            return String.Format("SELECT max(pyd_id) from table_paymentdebit");
        }
        public static string GetDeleteByDebitTransactionIDSQL(int drtrId)
        {
            return "delete from table_paymentdebit where drtr_id = " + drtrId;
        }
        public static string GetByDebitTransactionIDSQL(int drtrId)
        {
            return "select * from table_paymentdebit where drtr_id = " + drtrId;
        }
        public string GetAllSQL(DateTime fromDate, DateTime toDate, bool alldrTr
            , int drTrID, bool allCcy, int ccyID, string doctype, bool doctypeall, bool allVessel,
            int vesselID
            )
        {
            string alldrTrFilter = alldrTr ? "" : " and t.drtr_id = " + drTrID.ToString();
            string allCcyFilter = allCcy ? "" : " and t.ccy_id = " + ccyID.ToString();
            string allStasusFilter = doctypeall ? "" : " and t.pyd_doctype = '" + doctype + "'";
            string allVesselFilter = allVessel ? "" : " and ves.ves_id = " + vesselID.ToString();
            string sql = @"SELECT * FROM table_paymentdebit t
                            inner join table_currency ccy on ccy.ccy_id = t.ccy_id
                            inner join table_debittransaction drtr on drtr.drtr_id = t.drtr_id
                            inner join table_vessel ves on ves.ves_id = drtr.ves_id
                             where t.pyd_date between '" + fromDate.ToString("yyyy/MM/dd") + @"'
                            and '" + toDate.ToString("yyyy/MM/dd") + "'" + alldrTrFilter + allCcyFilter + allStasusFilter + allVesselFilter +
                            " order by t.pyd_date";
            return sql;
        }
    }
}
